package com.william.jdbcplus.core.toolkit;

import java.io.*;
import java.util.UUID;

/**
 * 文件处理类
 * @author niliwei
 */
public class ForFileUtils {
    /**
     * 创建文件
     *
     * @param fileName    文件名称
     * @param content 文件内容
     * @return 是否创建成功，成功则返回true
     */
    public static boolean createFile(String path, String fileName, String content) {
        Boolean bool = false;
        String filenameTemp = path + fileName;//文件路径+名称+文件类型
        File file = new File(filenameTemp);
        try {
            //如果文件不存在，则创建新的文件
            if (!file.exists()) {
                file.createNewFile();
                bool = true;
                System.out.println("success create file,the file is " + filenameTemp);
                //创建文件成功后，写入内容到文件里
                writeFileContent(path, fileName, content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bool;
    }

    /**
     * 向文件中写入内容
     *
     * @param path 文件路径
     * @param fileName 文件名称
     * @param content   写入的内容
     * @return
     * @throws IOException
     */
    public static boolean writeFileContent(String path, String fileName, String content) throws IOException {
        Boolean bool = false;
        String fileIn = content + "\r\n";//新写入的行，换行
        String temp = "";

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        String filepath = path + fileName;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();

            //文件原有内容
            for (int i = 0; (temp = br.readLine()) != null; i++) {
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            buffer.append(fileIn);

            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }

    /**
     * 删除文件
     *
     * @param path 文件路径
     * @param fileName 文件名称
     * @return
     */
    public static boolean delFile(String path, String fileName ) {
        Boolean bool = false;
        String filenameTemp = path + fileName;
        File file = new File(filenameTemp);
        try {
            if (file.exists()) {
                file.delete();
                bool = true;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return bool;
    }

    /**
     * 清空文件内容
     * @param fileName
     */
    public static boolean createAndWrite(String path, String fileName, String json) {
        String fileNameAndPath = path + fileName;
        File file =new File(fileNameAndPath);
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
            writeFileContent(path, fileName, json);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public static String readFileContent(String path, String fileName) {
        String fileNameAndPath = path + fileName;
        File file = new File(fileNameAndPath);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }


    public static void main(String[] args) {
        //生成文件路径
        String path = "D:\\file\\";
        createAndWrite(path,"myfile.json", "我的梦说别停留等待,就让光芒折射泪湿的瞳孔,映出心中最想拥有的彩虹,带我奔向那片有你的天空,因为你是我的梦 我的梦");
    }
}